package com.lanxin.zc.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lanxin.bx.entity.BxInformal;
import com.lanxin.bx.entity.BxInformalaccessary;
import com.lanxin.bx.entity.BxInformaldetail;
import com.lanxin.bx.entity.BxInformaltype;
import com.lanxin.bx.service.IBxInformalService;
import com.lanxin.bx.service.IBxInformalaccessaryService;
import com.lanxin.bx.service.IBxInformaldetailService;
import com.lanxin.bx.service.IBxInformaltypeService;
import com.lanxin.hr.entity.JcBasicdatavalue;
import com.lanxin.hr.entity.ZzDepartment;
import com.lanxin.hr.entity.ZzEmployee;
import com.lanxin.hr.service.IJcBasicdatavalueService;
import com.lanxin.hr.service.IZzDepartmentService;
import com.lanxin.hr.service.IZzEmployeeService;
import com.lanxin.judge.entity.LcFlow;
import com.lanxin.judge.service.ILcFlowService;
import com.lanxin.zc.entity.*;
import com.lanxin.zc.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.time.ZoneId;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 
 * @since 2025-07-16
 */
@Controller
@RequestMapping("/zc-assetmovedetails")
public class ZcAssetmovedetailsController {

    @Autowired
    private IZcAssetmoveapplyService zcAssetmoveapplyService;
    @Autowired
    private IZcAssetmovedetailsService zcAssetmovedetailsService;
    @Autowired
    private IZzEmployeeService zzEmployeeService;
    @Autowired
    private ILcFlowService lcFlowService;
    @Autowired
    private IJcBasicdatavalueService jcBasicdatavalueService;
    @Autowired
    private IZcAssetService zcAssetService;
    @Autowired
    private IZzDepartmentService zzDepartmentService;

    @RequestMapping("/zcAssetmoveapplyInfo")
    public ModelAndView zcAssetmoveapplyInfo(@RequestParam(value = "oid") Integer oid,
                                             @RequestParam(value = "s",required = false) String s) {
        ModelAndView modelAndView = new ModelAndView();

        // 1. 查询主表信息（移动申请单）
        ZcAssetmoveapply main = zcAssetmoveapplyService.getById(oid);
        modelAndView.addObject("main", main);

        // 2. 查询明细列表（移动明细）
        List<ZcAssetmovedetails> details = zcAssetmovedetailsService.list(
                new QueryWrapper<ZcAssetmovedetails>()
                        .eq("parentid", oid)
        );
        //3.资产名称
        for (ZcAssetmovedetails detail : details) {
            // 查询资产信息
            if (detail.getAssetId() != null) {
                ZcAsset asset = zcAssetService.getById(detail.getAssetId());
                detail.setAsset(asset);

                // 查询原保管人信息
                if (asset.getKeeper() != null) {
                    ZzEmployee keeper = zzEmployeeService.getById(asset.getKeeper());
                    detail.setKeeper(keeper);
                }

                // 查询原存放地信息
                if (asset.getDepositary() != null) {
                    JcBasicdatavalue storage = jcBasicdatavalueService.getById(asset.getDepositary());
                    detail.setStorage(storage);
                }
            }

            // 查询移动目的地信息
            if (detail.getTargetPlace() != null) {
                JcBasicdatavalue targetPlace = jcBasicdatavalueService.getById(detail.getTargetPlace());
                detail.setTargetPlaceObj(targetPlace);
            }
        }

        // 4. 查询人员和组织信息
        // 创建人
        if (main.getCreator() != null) {
            ZzEmployee creator = zzEmployeeService.getById(main.getCreator());
            modelAndView.addObject("creatorName", creator != null ? creator.getEmpName() : "");
        }
        // 申请人名称
        if (main.getProposer() != null) {
            ZzEmployee proposer = zzEmployeeService.getById(main.getProposer());
            modelAndView.addObject("proposerName", proposer != null ? proposer.getEmpName() : "");
        }
        // 部门名称
        if (main.getDeptid() != null) {
            ZzDepartment dept = zzDepartmentService.getById(main.getDeptid());
            modelAndView.addObject("deptName", dept != null ? dept.getDeptName() : "");
        }
        // 移动事由
        if (main.getMoveReason() != null) {
            JcBasicdatavalue reason = jcBasicdatavalueService.getById(main.getMoveReason());
            modelAndView.addObject("reasonName", reason != null ? reason.getName() : "");
        }
        // 流程名称
        if (main.getFlowid() != null) {
            LcFlow flow = lcFlowService.getById(main.getFlowid());
            modelAndView.addObject("flowName", flow != null ? flow.getFlowName() : "");
        }
       modelAndView.addObject("s", s);
        modelAndView.addObject("oid", oid);
        modelAndView.addObject("details", details);
        modelAndView.setViewName("zc/zcassetmoveapply/zcAssetmoveapplyInfo");

        return modelAndView;
    }


}
